package cn.zbzcode.hrm.web.controller;

import cn.zbzcode.hrm.utils.AjaxResult;
import cn.zbzcode.hrm.utils.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FastdfsController {
    //上传文件到fastdfs,返回文件的路径。
    @PostMapping("/fastdfs/upload")
    public AjaxResult upload(MultipartFile file){
        try {
            //拿到扩展名，使用用具包commons-io里面的FilenameUtils
            String extName = FilenameUtils.getExtension(file.getOriginalFilename());
            String filePath = FastDfsApiOpr.upload(file.getBytes() , extName);
            return AjaxResult.me().setResultObj(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败");
        }
    }
}